<template>
  <div class="container-fans">
    <el-card>
      <div slot="header">
        <my-bread>粉丝管理</my-bread>
      </div>
      <!-- tab组件 -->
      <el-tabs v-model="activeName" type="card">
        <el-tab-pane label="粉丝列表" name="list">
          <!-- 粉丝列表 -->
          <div class="fans-list">
            <!-- key的类型只能是字符串或数字,转换一下 -->
            <div class="fans-item" v-for="fans in fansList" :key="fans.id.toString()">
              <el-avatar :size="80" :src="fans.photo"></el-avatar>
              <p>{{fans.name}}</p>
              <el-button type="primary" plain size="mini">+关注</el-button>
            </div>
          </div>
          <!-- 分页 -->
          <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :current-page="reqParams.page"
            :page-size="reqParams.per_page"
            @current-change="changePager"
          ></el-pagination>
        </el-tab-pane>
        <el-tab-pane label="粉丝画像" name="chart">
          <div ref="dom" style="height:400px;width:600px"></div>
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
// 引入echarts图表
import echarts from "echarts";
export default {
  name: "fans-page",
  data() {
    return {
      activeName: "chart",
      fansList: [],
      total: 0,
      reqParams: {
        page: 1,
        per_page: 24
      }
    };
  },
  created() {
    this.getFansList();
  },
  mounted() {
    // 渲染echart图表
    this.initBar();
  },
  methods: {
    // 初始化柱状图
    async initBar() {
      const myChart = echarts.init(this.$refs.dom);
      const option = {
        color: ["#3398DB"],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow" // 默认为直线，可选为：'line'|'shadow'
          }
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true
        },

        xAxis: [
          {
            type: "category",
            // X轴刻度
            data: [],
            axisTick: {
              alignWithLabel: true
            }
          }
        ],
        yAxis: [{ type: "value" }],

        series: [
          {
            name: "直接访问",
            type: "bar",
            barWidth: "60%",
            // X轴数据
            data: []
          }
        ]
      };
      // 获取统计的数据
      const {
        data: { data }
      } = await this.$http.get("/statistics/followers");
      let arr1 = []; //X轴刻度
      let arr2 = []; //X轴数据
      for (let key in data.age) {
        arr1.push(key);
        arr2.push(data.age[key]);
      }
      // 获取的数据填充到option
      option.xAxis[0].data = arr1;
      option.series[0].data = arr2;
      myChart.setOption(option);
    },
    // 分页
    changePager(np) {
      this.reqParams.page = np;
      this.getFansList();
    },
    // 获取粉丝列表
    async getFansList() {
      const {
        data: { data }
      } = await this.$http.get("/followers", { params: this.reqParams });
      this.fansList = data.results;
      this.total = data.total_count;
    }
  }
};
</script>

<style scoped lang="less">
.fans-list {
  .fans-item {
    width: 100px;
    height: 150px;
    text-align: center;
    border: 1px dashed #ddd;
    padding-top: 5px;
    display: inline-block;
    margin-left: 25px;
    margin-bottom: 20px;
    p {
      margin: 5px auto;
      font-size: 14px;
    }
  }
}
</style>